#!/bin/bash
#
# Slackware build script for IntelliJIDEA
# Written by Mario Antunes (mariolpantunes@gmail.com)
# This script is just a binary repackaging.

PRGNAM=idea
ARCNAM=ideaIC
VERSION=${VERSION:-13.1.3}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
ARCH=noarch

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG/opt/ $OUTPUT
cd $PKG/opt/
tar xvf $CWD/$ARCNAM-$VERSION.tar.gz
mv * $PRGNAM

mkdir -p $PKG/etc/profile.d
cat << EOF > $PKG/etc/profile.d/$PRGNAM.sh
#!/bin/sh
export IDEA_HOME=/opt/idea
export PATH=\$PATH:\$IDEA_HOME/bin
EOF
cat << EOF > $PKG/etc/profile.d/$PRGNAM.csh
#!/bin/csh
setenv IDEA_HOME /opt/idea
setenv PATH \$PATH:\$IDEA_HOME/bin
EOF
chmod 755 $PKG/etc/profile.d/*

install -D -m 0644 $CWD/$PRGNAM.desktop $PKG/usr/share/applications/$PRGNAM.desktop
install -D -m 0644 $PKG/opt/$PRGNAM/bin/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# Prevent fsnotifer and fsnotifier64 from being striped
find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
    | grep -v "fsnotifier" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cd $PKG/opt/$PRGNAM
mv *.txt license $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
